<?php
namespace Admin\Controller;
use Think\Controller;
class FriendsController extends Controller{
	public function index(){
	    $Friendsmodel=M('Friends');
	    $p=isset($_GET['p'])?$_GET['p']:1;
	    $pnum=15;
	    //分页查询
	    $friends=$Friendsmodel->page($p,$pnum)->order('fri_id desc')->select();
	    //获取总页数
	    $count=$Friendsmodel->where("1")->count();
	    //实例化分页对象
	    $page=new \Think\Page($count,$pnum,$p);
	    //获取分页信息
	    $show=$page->show();
	    //发送分页信息到模版
	    $this->assign("pageinfo",$show);
	    //发送查询结果到页面
	    $this->assign('friends',$friends);
	    $this->display();
	}
	public function insert(){
	    $data['link_name'] = deepspecialchars(trim($_POST['link_name']));
	    //接收网站名称
	    $data['link_url'] = deepspecialchars(trim($_POST['link_url']));
	    //接收链接地址
	    $Friendsmodel=M('Friends');
	    if (!empty($data['link_name'])&&!empty($data['link_url'])&&$Friendsmodel->add($data)) {
	    //如果网站名称和链接地址不为空并且执行数据库添加语句成功则提示成功并跳转
	        $this->success('新增成功',U('Friends/index'));
	    }else {
	        $this->error("添加失败!");
	    }
	}
	public function update(){
	    $data['fri_id'] = $_POST['fri_id']+0;
	    //接收友联ID
	    $data['link_name'] = deepspecialchars(trim($_POST['link_name']));
	    //接收网站名称
	    $data['link_url'] = deepspecialchars(trim($_POST['link_url']));
	    //接收链接地址
	    $Friendsmodel=M('Friends');
	    $result = $Friendsmodel->where("fri_id = {$data['fri_id']}")->save($data);
	    //修改
	    if($result){
	        $this->success("修改成功!",U('Friends/index'));
	    }
	    else{
	        $this->error("修改失败!",U('Friends/index'));
	    }
	    
	}
	public function delete(){
	    $data['fri_id'] = $_GET['fri_id']+0;
	    //接收友联ID
	    $Friendsmodel=M('Friends');
	    $result = $Friendsmodel->where("fri_id={$data['fri_id']}")->delete();
	    //删除
	    if($result){
	        $this->success("删除成功!",U('Friends/index'));
	    }
	    else{
	        $this->error("删除失败!",U('Friends/index'));
	    }
	}
}